package stu.hxl.mpdemo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import stu.hxl.mpdemo.dao.entity.OtherInfo;
import stu.hxl.mpdemo.dao.entity.User;
import stu.hxl.mpdemo.enums.UserTypeEnum;
import stu.hxl.mpdemo.enums.VipTypeEnum;
import stu.hxl.mpdemo.service.IUserService;
import stu.hxl.mpdemo.vo.UserVo;

import java.util.Arrays;
import java.util.List;

/**
 * @author sharloon
 * @date 2019/11/15
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private IUserService userService;

    /**
     * 测试写入枚举值
     *
     * @return
     */
    @GetMapping("/1")
    public Boolean test1() {

        //两个枚举分别往数据库存A 和 1(枚举中使用了@EnumValue)
        User user = new User("sharloon", 27, "sharloon@163.com", UserTypeEnum.A, VipTypeEnum.LEVEL_1);

        return userService.save(user);
    }

    /**
     * 1.测试返回枚举参数(带枚举的详细信息,eg："vipType":{"type":1,"desc":"一级"})
     * 2.测试读取数据库中的json数据，映射到实体中的对象属性
     *
     * @return
     */
    @GetMapping("/2")
    public UserVo test2(String name) {

        User user = userService.getOne(Wrappers.<User>query().lambda()
                .eq(User::getName, name).last("limit 1"));

        return new UserVo(user.getId(), user.getName(), user.getAge(), user.getEmail(), user.getUserType(),
                user.getVipType(), user.getOtherInfo());
    }

    /**
     * 测试数据库中json，实体中使对象 -- 新增数据
     *
     * @return
     */
    @GetMapping("/3")
    public Boolean test3() {

        OtherInfo otherInfo1 = new OtherInfo();
        otherInfo1.setSex("1");
        otherInfo1.setCity("深圳");

        OtherInfo otherInfo2 = new OtherInfo();
        otherInfo2.setSex("2");
        otherInfo2.setCity("上海");

        List<OtherInfo> infoList = Arrays.asList(otherInfo1, otherInfo2);

        User user = new User("sharlin", 22, "sharlon@163.com", infoList);

        return userService.save(user);
    }

    /**
     * 批量新增数据
     *
     * @return
     */
    @GetMapping("/4")
    public Boolean test4() {

        int i = 4369735;

        do {

            userService.save(new User("sharloon" + i, i, "sharlon" + i + "@163.com", null));

            i++;

            System.out.println(i);

        } while (i < 10000000);

        return Boolean.TRUE;

    }

    /**
     * 分页
     *
     * @return
     */
    @GetMapping("/5")
    public IPage<User> test5(@RequestParam Integer currentPage, @RequestParam Integer pageSize) {

        return userService.getData(currentPage, pageSize);
    }

}
